package com.dwxt.community.admin.service;

import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.util.IDUtils;
import com.dwxt.community.dao.AdLocationMapper;
import com.dwxt.community.dao.AdLocationTypeMapper;
import com.dwxt.community.dao.InformationMapper;
import com.dwxt.community.entity.AdLocation;
import com.dwxt.community.entity.AdLocationExample;
import com.dwxt.community.entity.vo.InfomationLocationVo;
import com.dwxt.community.exception.DefinedException;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class InformationLocationService {

    @Autowired
    private AdLocationMapper adLocationMapper;
    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private AdLocationTypeMapper adLocationTypeMapper;

    public BaseResult get(Integer pageNum, Integer pageSize, String type) {
        PageHelper.startPage(pageNum,pageSize);
        List<InfomationLocationVo> infomationLocationVo = adLocationMapper.selectByLocationType(type);
        return BaseResult.ok(infomationLocationVo);
    }

    public BaseResult del(String id) {
        adLocationMapper.deleteByPrimaryKey(id);
        return BaseResult.ok();
    }

    public BaseResult delInformationForLocation(String id) {
        AdLocation adLocation = adLocationMapper.selectByPrimaryKey(id);
        adLocation.setInformationId(null);
        adLocationMapper.updateByPrimaryKey(adLocation);
        return BaseResult.ok();
    }

    public BaseResult insert( AdLocation adLocation) {
        if (ObjectUtils.isEmpty(adLocationTypeMapper.selectByPrimaryKey(adLocation.getType()))){
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        if (ObjectUtils.isEmpty(informationMapper.selectByPrimaryKey(adLocation.getInformationId()))){
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        yanZhenInformation(adLocation.getInformationId(), adLocation);
        Date date = new Date();
        adLocation.setCreateTime(date);
        adLocation.setId(IDUtils.genItemId());
        adLocation.setUpdateTime(date);
        adLocationMapper.insertSelective(adLocation);
        return BaseResult.ok();
    }

    public BaseResult update(String locationId, String informationId) {
        AdLocation adLocation = adLocationMapper.selectByPrimaryKey(locationId);
        if (ObjectUtils.isEmpty(adLocation) || ObjectUtils.isEmpty(informationMapper.selectByPrimaryKey(informationId))){
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        yanZhenInformation(informationId, adLocation);
        adLocation.setInformationId(informationId);
        adLocationMapper.updateByPrimaryKey(adLocation);
        return BaseResult.ok();
    }

    private void yanZhenInformation(String informationId, AdLocation adLocation) {
        AdLocationExample adLocationExample = new AdLocationExample();
        AdLocationExample.Criteria criteria = adLocationExample.createCriteria();
        criteria.andInformationIdEqualTo(informationId);
        criteria.andTypeEqualTo(adLocation.getType());
        if (ObjectUtils.isNotEmpty(adLocationMapper.selectByExample(adLocationExample))) {
            throw new DefinedException(BaseResult.build(StatusCode.INFORMATION_IS_USEED));
        }
    }

    public BaseResult getLocationType() {
        return BaseResult.ok(adLocationTypeMapper.selectByExample(null));
    }
}
